package io;

import java.io.File;
import java.util.Scanner;

public class Demo11 {
    public static void main(String[] args) {
        System.out.println("输入你要删除的路径");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        File file = new File(s);
        //判断文件是不是目录
        if(!file.isDirectory()) {
            System.out.println("非法目录");
            return;
        }
        //要删除目标文件要包含的子串
        System.out.println("输入删除文件包含特征: ");
        String target = scanner.nextLine();
        //遍历目录下的文件
        f1(file, target);

    }

    private static void f1(File file, String target) {
        File[] list = file.listFiles();
        for (File f : list) {
            if(f.isFile()) {
                //判断是不是要删除的文件
                isDelete(target, f);
            }else {
                //目录,继续递归
                f1(f, target);
            }
        }
    }

    private static void isDelete(String target, File f) {
        if(f.getName().contains(target)) {
            System.out.println("是否要删除该路径的文件"+f.getAbsolutePath());
            System.out.println("输入Y/N: ");
            Scanner scanner = new Scanner(System.in);
            String s = scanner.next().toUpperCase();
            if(s.toUpperCase().equals("Y")) {
                f.delete();
            }
        }

    }
}
